package com.zhiwang.planet.ui.my

import android.graphics.Bitmap
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.planet.processor.PageRoute
import com.zhiwang.planet.App
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvp.p.GraphCodePresenter
import com.zhiwang.planet.mvp.p.UserPresenter
import com.zhiwang.planet.mvp.v.*
import com.zhiwang.planet.mvvm.model.entity.User
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.ui.LoginActivity
import com.zhiwang.planet.util.Toaster
import com.zhiwang.planet.util.quickStartActivity
import com.zhiwang.planet.util.suicideAfterTransition
import kotlinx.android.synthetic.main.activity_modify_password.*
import java.util.*
import javax.inject.Inject

@PageRoute(pageid = 0xff01, path = "account/modify_password")
class ModifyPasswordActivity : BaseActivity(), UserInfoView, SmsCodeView, GraphView, ChangePasswordView, FindPasswordView {
    override fun onPasswordFind() {
        Toaster.message(R.string.message_pwd_change_success)
        quickStartActivity(LoginActivity::class.java)
        suicideAfterTransition()
    }

    override fun onPasswordChanged() {
        Toaster.message(R.string.message_pwd_change_success)
        quickStartActivity(LoginActivity::class.java)
        suicideAfterTransition()
    }

    @Inject
    lateinit var userPresenter: UserPresenter

    @Inject
    lateinit var graphPresenter: GraphCodePresenter

    //false ：修改密码 ；true：找回密码
    var isFindMode = false
    var seed = Random().nextLong().toString()
    override fun initInjection() {
        DaggerUserComponent
                .builder()
                .accountModule(AccountModule(this))
                .appComponent((application as App).appComponent)
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)
        userPresenter.userInfoView = this
        userPresenter.smsCodeView = this
        graphPresenter.graphView = this
        userPresenter.findPasswordView = this
        userPresenter.changePasswordView = this
//        userPresenter.userInfoView = this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_modify_password)
        setSupportActionBar(toolbar)
        userPresenter.queryUserInfo()
        observeTextChangeWithTargetButtonEnableStatus(arrayOf(graphCodeEditor, password, confirmPassword), submit)
    }

    //获取修改密码验证码
    fun getSmsCode(v: View) {
        val phone = tel.text.toString()
        val graphCode = graphCodeEditor.text.toString()
        if (TextUtils.isEmpty(phone)) {
            Toaster.message("手机号不能为空")
            return
        } else if (TextUtils.isEmpty(graphCode)) {
            Toaster.message("图形验证码不能为空")
            return
        }
        if (isFindMode) {
            userPresenter.getFindPwdSmsCode(seed, graphCode, phone)
        } else userPresenter.getChangePwdSmsCode(seed, graphCode, phone)
    }


    override fun onSmsSend() {
        Toaster.message(R.string.message_sms_send)
        captchaTextView.start()
    }

    fun confirmModifyPassword(v: View) {
        val phone = tel.text.toString()
        val password = password.text.toString()
        val smscode = smsCodeEditor.text.toString()
        if (isFindMode) {
            if (checkParameter(true))
                userPresenter.findPassword(phone, smscode,password)
        } else {
            if (checkParameter(false))
                userPresenter.changePassword(phone, password, smscode)
        }
    }


    override fun onUserInfoLoad(user: User?) {
        if (user == null) {
            isFindMode = true
            toolbar.setTitle("找回密码")
            graphPresenter.getFindGraphCode(seed)
        } else {
            isFindMode = false
            toolbar.setTitle("修改密码")
            tel.isEnabled = false
            tel.setText(user.phone)
            graphPresenter.getChangeGraphCode(seed)
        }
    }

    fun getGraphCode(v: View) {
        if (isFindMode)
            graphPresenter.getFindGraphCode(seed)
        else graphPresenter.getChangeGraphCode(seed)
    }

    override fun onGraphCodeLoad(b: Bitmap) {
        gvcodeBtn.setImageBitmap(b)
    }

    private fun checkParameter(needPhone: Boolean): Boolean {
        val phone = tel.text.toString()
        val captchaString = graphCodeEditor.text.toString()
        val passwordString = password.text.toString()
        val confirmedPasswordString = confirmPassword.text.toString()
        if (needPhone) {
            if (TextUtils.isEmpty(phone)) {
                Toaster.message("手机号不能为空")
                return false
            }
        } else if (TextUtils.isEmpty(captchaString)) {
            Toaster.message(R.string.captcha_empty)
            return false
        } else if (TextUtils.isEmpty(passwordString)) {
            Toaster.message(R.string.password_empty)
            return false
        } else if (TextUtils.isEmpty(confirmedPasswordString)) {
            Toaster.message(R.string.please_input_new_pwd_again)
            return false
        } else if (!TextUtils.equals(passwordString, confirmedPasswordString)) {
            Toaster.message(R.string.password_inconsistent)
            return false
        }
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return super.onSupportNavigateUp()
    }
}


